package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserParam;
import cn.wolfcode.luowowo.domain.Person;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.redis.RedisService.IStrategyStatisVORedisService;
import cn.wolfcode.luowowo.redis.RedisService.IUserInfoRedisService;
import cn.wolfcode.luowowo.repository.PersonRepository;
import cn.wolfcode.luowowo.service.IPersonService;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.vo.PersonVo;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("person")
//DestinationsController
public class PersonController {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IPersonService personService;

    @Autowired
    private StringRedisTemplate template;

    @Autowired
    private PersonRepository personRepository;

    //获取某个用户的信息
    @GetMapping("/list")
    public Object list() {
        List<Person> list = personService.list();
        return JsonResult.success(list);
    }

    @GetMapping("/Page")
    public Object Page() {
        personService.save();
        List<Person> list = personService.list();
        Query query = new Query();
        Pageable pageRequest = PageRequest.of(0, 8, Sort.Direction.ASC, "id");
        query.with(pageRequest);
        List<Person> people = mongoTemplate.find(query, Person.class);
        return JsonResult.success(people);
    }

    @GetMapping("/getTotalView")
    public Object getTotalView() {
//        Long totalView = null;
//        if (template.hasKey("totalView")) {
//            totalView = template.opsForValue().increment("totalView");
//        } else {
//            template.opsForValue().set("totalView", JSON.toJSONString(0));
//        }

        long count = personRepository.count();
        return JsonResult.success(count);
    }

    @GetMapping("/getTodayView")
    public Object getTodayView() {
//        Long todayView = null;
//        if (template.hasKey("todayView")) {
//            todayView = template.opsForValue().increment("todayView");
//        } else {
//            template.opsForValue().set("todayView", JSON.toJSONString(0));
//        }
//        return JsonResult.success(todayView);
        long count = personRepository.count();
        return JsonResult.success(count);
    }


}
